

package com.lry.rpc.starter.extend;

import com.lry.rpc.starter.annotation.EnableConfigurationBeanBindings;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;

public class ConfigurationBeanBindingsRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
    private ConfigurableEnvironment environment;

    public ConfigurationBeanBindingsRegister() {
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableConfigurationBeanBindings.class.getName()));
        AnnotationAttributes[] annotationAttributes = attributes.getAnnotationArray("value");


        ConfigurationBeanBindingRegistrar registrar = new ConfigurationBeanBindingRegistrar();
        registrar.setEnvironment(this.environment);
        for(AnnotationAttributes element:annotationAttributes) {
            registrar.registerConfigurationBeanDefinitions(element, registry);
        }

    }

    @Override
    public void setEnvironment(Environment environment) {
        Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
        this.environment = (ConfigurableEnvironment)environment;
    }
}
